<?php
namespace Rate\Controller;
use Todo\Controller\ApiController;

class ScoreController extends ApiController {

    /**
     * 一次性保存全部
     */
   public function saves(){
       $projectId = I('post.project_id');
       if(!$projectId) $this->apiError("参数不完整，没有项目信息!");

       $userId = I('post.user_id');
       if(!$userId) $this->apiError("参数不完整，没有被评用户信息!");

       $record = D('Todo/Rate/Record')->checkRecord($projectId, $userId, $this->_user['id']);

       if(!$record)$this->apiError('发生错误!');

       $this->_loadModel();
       $normList = $this->_model->query("SELECT n.* FROM rate_norm n JOIN rate_template t ON t.id=n.tpl_id JOIN rate_project p ON p.tpl_id=t.id WHERE p.id=".$projectId);
       $total = 0;
       foreach ($normList as $norm){
           $val = I('post.norm_'.$norm['id']);
           if(!$this->_model->saveScore($norm, $record, $val)) $this->apiError("发生错误:". $this->_model->getError());
           $total += $val;
       }

       $record['total'] = $total;
       D('Todo/Rate/Record')->save($record);
       $this->apiSuccess("保存成功!");
   }

    /**
     * 提交一次打分项目
     */
    public function submit(){
        $projectId = I('post.project_id');
        if(!$projectId) $this->apiError("参数不完整，没有项目信息!");
    }
}